package com.mediacloud.app.newsmodule.fragment.audio.live

import android.os.Bundle
import com.mediacloud.app.model.activity.BaseBackActivity
import com.mediacloud.app.model.interfaces.DataInvokeCallBack
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.model.news.ArticleListData
import com.mediacloud.app.model.news.BaseMessageReciver
import com.mediacloud.app.model.news.CatalogItem
import com.mediacloud.app.model.utils.ViewUtils
import com.mediacloud.app.nav2.fragment.HqyNavFragment
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.activity.IChangeProgram
import com.mediacloud.app.newsmodule.adaptor.audio.live.AudioLiveColumnHomeRecommendListAdapter
import com.mediacloud.app.newsmodule.utils.NewsListDataInvoker
import com.mediacloud.app.util.ClickUtils
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.fragment_program_home_recommend_list.*
import java.util.*

/**
 * 广播  节目单 主页列表
 */
class AudioLiveColumnHomeRecommendListFragment(var iChangeProgram: IChangeProgram) : HqyNavFragment(), OnRefreshLoadMoreListener {
    private val mDataList: MutableList<ArticleItem> = ArrayList()
    private var mAdapterProgramme: AudioLiveColumnHomeRecommendListAdapter? = null
    private var mPageIndex: Int = 1
    private var PAGE_SIZE: Int = 20
    private var programCatalogItem: CatalogItem? = null
    var dataInvoker: NewsListDataInvoker? = null
    var dataCallback: BaseDataInvokeCallBack<out BaseMessageReciver?>? = null


    companion object {
        fun newInstance(title: String? = "" , articleItem: ArticleItem? ,
                        catalogItem: CatalogItem? ,iChangeProgram: IChangeProgram): AudioLiveColumnHomeRecommendListFragment {
            val fragment = AudioLiveColumnHomeRecommendListFragment(iChangeProgram)
            val bundle = Bundle()
            bundle.putString("title", title)
            bundle.putParcelable("data",articleItem)
            bundle.putParcelable("catalogItem", catalogItem)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getLayoutResID(): Int {
        return R.layout.fragment_program_home_recommend_list
    }

    override fun initArgs() {
        super.initArgs()
        programCatalogItem=arguments?.getParcelable("catalogItem")
        initDataInvoker()
    }
    protected fun initDataInvoker() {
        dataCallback = BaseNewsDataInvokeCallBack()
        dataInvoker = NewsListDataInvoker(dataCallback)
    }

    override fun initView() {
        loadingView = mLoadingView
        initStateView()
    }

    override fun initOther() {
        super.initOther()

        showStateView(TYPE_LOADING, false)
        initRecyclerView()
        initListener()
    }

    private fun initRecyclerView() {
        mAdapterProgramme = AudioLiveColumnHomeRecommendListAdapter(mDataList)
        recyclerview.layoutManager = ViewUtils.generateRecyclerLinearLayoutManager(context,true)
        mAdapterProgramme?.bindToRecyclerView(recyclerview)
    }

    private fun initListener() {
        refreshLayout.setOnRefreshLoadMoreListener(this)
        loadingView.setOnClickListener {
            if (TYPE_LOADING != currentState) {
                showStateView(TYPE_LOADING, false)
                onRefresh()
            }
        }
        mAdapterProgramme?.setOnItemClickListener { adapter, view, position ->
            ClickUtils.delayClickable(view)
            mAdapterProgramme?.data?.get(position)?.let {
                iChangeProgram.changeProgramToUpdateUI(it)
            }
        }

    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        onRefresh()

    }

    private fun onRefresh() {
        mPageIndex = 1
        getList()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        onLoadMore()
    }

    private fun onLoadMore() {
        mPageIndex++
        getList()
    }

    private var isFirstLoadingData = true

    override fun onResume() {
        super.onResume()

        if (isAdded && hadChoosed) {
            if (isFirstLoadingData) {
                onRefresh()
                isFirstLoadingData = false
            }
        }
    }

    override fun choosed() {
        super.choosed()
        if (activity != null) {
            if (isFirstLoadingData) {
                onRefresh()
                isFirstLoadingData = false
            }
        }
    }


    fun setEnableRefresh(enable: Boolean) {
        if (activity == null || requireActivity().isFinishing) {
            return
        }
        refreshLayout.setEnableRefresh(enable)
    }

    fun getList() {
        dataInvoker?.getContentListWithAd(programCatalogItem?.id, mPageIndex, PAGE_SIZE,null)
    }
    inner abstract class BaseDataInvokeCallBack<T> : DataInvokeCallBack<T> {
        override fun fault(data: Any?) {
            if (activity == null || requireActivity().isFinishing) {
                return
            }
            if (mPageIndex == 1) {
                refreshLayout.finishRefresh()
                if (data != null && (mAdapterProgramme?.data == null || mAdapterProgramme?.data?.size == 0)) {
                    showStateView(BaseBackActivity.TYPE_NET_NOT_GIVE_FORCE, false)
                } else if (mAdapterProgramme?.data == null || mAdapterProgramme?.data?.size == 0) {
                    showStateView(BaseBackActivity.TYPE_NO_CONTENT, false)
                } else {
                    closeStateView()
                }
            } else {
                refreshLayout.finishLoadMore()
                if (mPageIndex > 1) {
                    mPageIndex--
                }
            }
        }
    }
    inner class BaseNewsDataInvokeCallBack : BaseDataInvokeCallBack<ArticleListData?>() {
        override fun success(result: ArticleListData?) {
            if (activity == null || requireActivity().isFinishing) {
                return
            }

            var isHasNextPage: Boolean = false
            if (mPageIndex == 1) {
                refreshLayout.finishRefresh()
                mDataList.clear()
                mAdapterProgramme?.replaceData(mDataList)
            } else {
                refreshLayout.finishLoadMore()
            }
            val tempList:MutableList<ArticleItem> = ArrayList()
            result?.let {
                if (it.state) {

                    isHasNextPage = result.more
                    try {
                        result.articleList?.let {
                            tempList.addAll(it)
                        }
                    }catch (e:Exception){
                        e.printStackTrace()
                    }
                }
            }

            mAdapterProgramme?.addData(tempList)
            if (mPageIndex == 1) {
                if (mAdapterProgramme?.data == null || mAdapterProgramme?.data?.size == 0) {
                    showStateView(BaseBackActivity.TYPE_NO_CONTENT, false)
                } else {
                    closeStateView()
                }
            }
            if (isHasNextPage) {
                refreshLayout.setEnableLoadMore(true)
                refreshLayout.setNoMoreData(false)
            } else {
                refreshLayout.setNoMoreData(true)
            }

        }

    }
}